package tool;

public class Complex {
	private final double re;
	private final double im; 

	public Complex(double real, double imag) {
		this.re = real;
		this.im = imag;
	}

	public String toString() {
		if (im == 0)
			return re + "";
		if (re == 0)
			return im + "i";
		if (im < 0)
			return re + " - " + (-im) + "i";
		return re + " + " + im + "i";
	}
	
    public Complex times(Complex b) {
        Complex a = this;
        double real = a.re * b.re - a.im * b.im;
        double imag = a.re * b.im + a.im * b.re;
        return new Complex(real, imag);
    }

    public Complex times(double alpha) {
        return new Complex(alpha * re, alpha * im);
    }
    
    public Complex exp() {
        return new Complex(Math.exp(re) * Math.cos(im), Math.exp(re) * Math.sin(im));
    }
    
    public double getReal() {
    	return re;
    }
    public double getImag() {
    	return im;
    }
    
    public Complex addComplex(Complex comp) {
    	return new Complex(this.getReal() + comp.getReal(), this.getImag()+comp.getImag());
    }

    public Complex division (Complex comp) {
    	double a = (this.re*comp.re + this.im*comp.im) / (Math.pow(comp.re, 2) + Math.pow(comp.im, 2));
    	double b = (this.im*comp.re - this.re*comp.im) / (Math.pow(comp.re, 2) + Math.pow(comp.im, 2));
    	return new Complex(a, b);
    }
    public Complex add(double r) {
    	return new Complex(this.re +r, this.im);
    }
}
